package net.xiduth.game.player.content.events.raiding;

import java.util.TimerTask;

import net.xiduth.cores.CoresManager;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.npc.NPC;
import net.xiduth.game.player.Player;
import net.xiduth.utils.Logger;

/**
 * 
 * @author Jae
 * 
 * Handles Sunfreet events.
 * 
 */

public class SunfreetEvent {

	public static enum Delay {

		LUNAR_ISLE(3600000), DRAYNOR(1800000), FALADOR(7200000), BANDIT_CAMP(
				6200000), BURTHORPE(3200000), ELITE_CAVE(2200000);

		public final int time;

		Delay(int time) {
			this.time = time;
		}

		public int getTime() {
			return time;
		}
	}

	public static boolean ongoingLunar = false;
	
	public static boolean ongoingDraynor = false;
	
	public static boolean ongoingFalador = false;
	
	public static boolean ongoingBanditCamp = false;
	
	public static boolean ongoingBurth = false;
	
	public static boolean ongoingElite = false;
	
	public static final void killEvents() {
		ongoingLunar = false;
		ongoingDraynor = false;
		ongoingFalador = false;
		ongoingBanditCamp = false;
		ongoingBurth = false;
		ongoingElite = false;
		Logger.log("Sunfreet", "All events killed.");
	}
	public static final void initSunfreetEvents() {
		Logger.log("Sunfreet", "Reading data...");
		initRefresh();
		initAll();
		Logger.log("Sunfreet", "Events initiated.");
	}

	public static final void initAll() {
		initLunarSunfreet();
		initDraynorSunfreet();
		initDesertSunfreet();
		initFaladorSunfreet();
		initBurthorpeSunfreet();
		initEliteSunfreet();
	}
	
	public static final void initLunarSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Lunar Isle!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Lunar Isle to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(2109, 3890, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(2112, 3898, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(2113, 3888, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Lunar Isle: Raid initiated.");
					ongoingLunar = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.LUNAR_ISLE.getTime());
	}

	public static final void initFaladorSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Falador!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Falador to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(2965, 3386, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(2962, 3382, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(2968, 3382, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Falador: Raid initiated.");
					ongoingFalador = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.FALADOR.getTime());
	}

	public static final void initDraynorSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Draynor!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Draynor to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(3130, 3311, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(3130, 3318, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(3137, 3311, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Draynor: Raid initiated.");
					ongoingDraynor = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.DRAYNOR.getTime());
	}

	public static final void initBurthorpeSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Burthorpe!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Burthorpe to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(2879, 3554, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(2877, 3560, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(2880, 3553, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Burthorpe: Raid initiated.");
					ongoingBurth = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.BURTHORPE.getTime());
	}

	public static final void initDesertSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Bandit Camp!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Bandit Camp to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(3221, 2940, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(3215, 2944, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(3211, 2934, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Bandit Camp: Raid initiated.");
					ongoingBanditCamp = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.BANDIT_CAMP.getTime());
	}

	public static final void initEliteSunfreet() {
		CoresManager.fastExecutor.schedule(new TimerTask() {
			@Override
			public void run() {
				try {
					initRefresh();
					World.sendWorldMessage(
							"<img=7><col=FF0000>News: Sunfreet's army have raided Falador!",
							false);
					World.sendWorldMessage(
							"<img=7><col=FF0000>Teleport to Falador to join the battle!",
							false);
					World.spawnNPC(15222, new WorldTile(2965, 3386, 0), -1,
							true, true);
					World.spawnNPC(15172, new WorldTile(2962, 3382, 0), -1,
							true, true);
					World.spawnNPC(15174, new WorldTile(2968, 3382, 0), -1,
							true, true);
					Logger.log("Sunfreet", "Falador 2: Raid initiated.");
					ongoingFalador = true;
				} catch (Throwable e) {
					Logger.handle(e);
				}
			}
		}, 0, Delay.ELITE_CAVE.getTime());
	}

	public static final void initRefresh() {
		for (NPC n : World.getNPCs()) {
			if (n == null || n.getId() != 15222)
				continue;
			n.sendDeath(n);
		}
		for (NPC n : World.getNPCs()) {
			if (n == null || n.getId() != 15172)
				continue;
			n.sendDeath(n);
		}
		for (NPC n : World.getNPCs()) {
			if (n == null || n.getId() != 15174)
				continue;
			n.sendDeath(n);
		}
		for (Player p : World.getPlayers()) {
			p.getMusicsManager().playMusic(361);
		}
		killEvents();
		Logger.log("Sunfreet", "Refresh task completed.");
	}
}
